﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Text;
using System.Diagnostics;
using System.Threading;
using Lime;

namespace Clandestine.Graphics.Text
{
    public class Font
    {
        private System.Drawing.Font f;
        private System.Drawing.Graphics grtmp;
        private PrivateFontCollection pfc;

        public int Height
        {
            get
            {
                return (int)Math.Round(grtmp.MeasureString("M", f).Width / Fonts.PrecisionScale);
            }
        }

        //private int lineSpacing = 5;

        //public int LineSpacing
        //{
        //    get { return lineSpacing; }
        //}

        public bool UppercaseOnly;

      	/// <summary>
        /// Creates a Font object from a TrueType font file.
        /// </summary>
        public Font(string fontTtf, int textSizePt, bool uppercaseOnly)
        {
            if (!File.Exists(fontTtf))
                throw new FileNotFoundException("Could not find the font file "
                    + "specified in the arguments.", fontTtf);

            grtmp = System.Drawing.Graphics.FromImage(new Bitmap(1, 1)); // used for measuring strings w/o having to create another new bitmap
                  
            //this.lineSpacing = (int)(lineSpacing * Fonts.PrecisionScale); // TODO: ACTUALLY USE THIS
            textSizePt = (int)(textSizePt * Fonts.PrecisionScale);
            UppercaseOnly = uppercaseOnly;            

            pfc = new PrivateFontCollection();
            pfc.AddFontFile(fontTtf);
            Debug.Assert(pfc.Families.Length == 1, "No support for TTF files with >1 family!");

            f = new System.Drawing.Font(pfc.Families[0], textSizePt, FontStyle.Bold, GraphicsUnit.Pixel);
        }

        ~Font()
        {
            GC.KeepAlive(f);
            GC.KeepAlive(grtmp);
            GC.KeepAlive(pfc); // actually important
        }

        private string fixNewlines(string input)
        {
            return input.Replace("\r\n", "\n").Replace("\r", "");
        }
        
        public TextSprite DrawText(Graphics graphics, string text, params object[] formatting)
        {
        	return DrawText(graphics, String.Format(text, formatting));
        }

        public TextSprite DrawText(RenderList list, string text, params object[] formatting)
        {
            return DrawText(list, String.Format(text, formatting));
        }

		public TextSprite DrawText(Graphics graphics, string text)
		{
			return DrawText(graphics.DefaultRenderList, text);
		}
        public TextSprite DrawText(RenderList list, string text)
        {
            return new TextSprite(list, grtmp, f, (UppercaseOnly ? fixNewlines(text).ToUpper() : fixNewlines(text)));
        }

        public int GetStringWidth(string text)
        {
            return (int)Math.Round(grtmp.MeasureString(text, f).Width / Fonts.PrecisionScale);
        }
    }
}
